package cn.edu.scau.common.utils;

import cn.edu.scau.common.setting.IShiroConst;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * @className: RedisUtils
 * @description
 * @author: yuxiang
 * @date: 2020/6/16 12:13
 * @version:V1.0
 */
@Component
public class UserTokenRedisUtils {

    private static final String USERTOKEN_KEYPREFIX = "uid:token:";

    //缓存
    private static CacheManager cacheManager = CacheManager.newInstance();
    private static Long viewArticleTime = System.currentTimeMillis();

    private static Ehcache cache = cacheManager.getEhcache("userToken");
    private static Element element;

    public static boolean isExistedKey(String uid) {

        if (uid == null)
            return false;
        String key = USERTOKEN_KEYPREFIX + uid;
        //查询缓存
        element = cache.get(key);
        try {
            if (element!=null){
                System.out.println("token缓存===》》》"+key);
                String value = (String) element.getObjectValue();
                System.out.println("token缓存" + value);
                if (StringUtils.isNotBlank(value) && StringUtils.isNotEmpty(value)) {
                    return true;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public static String getValueByKey(Integer uid) {
        if (uid == null)
            return null;
        String key = USERTOKEN_KEYPREFIX + uid;
        //查询缓存
        if (cache.get(key)!=null){
            element = cache.get(key);
        }
        try  {
            if (element!=null){
                System.out.println("element" + element);
                String value = (String) element.getObjectValue();
                System.out.println("token缓存" + value);
                return value;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String setToken(Integer uid) {
        if (uid == null) {
            return null;
        }
        String key = USERTOKEN_KEYPREFIX + uid;
        String token = UUID.randomUUID().toString();

        try {
            //设置缓存
            Element e = new Element(key,token);
            //设置存活时间
            e.setTimeToLive(IShiroConst.EXPIRE_TIME);
            cache.put(e);
            element = cache.get(key);
            //查询缓存
            String value2 = (String) element.getObjectValue();
            System.out.println(key+"设置缓存缓存"+value2);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return token;
    }

    public static void delToken(Integer uid) {
        if (uid == null) {
            return;
        }
        String key = USERTOKEN_KEYPREFIX + uid;
        try {
            //查询缓存
            element = cache.get(key);
            if (element!=null){
                //如果缓存不为空
                //删除缓存
                cache.remove(key);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
